<?php
/**
 * Remove a Query or Only Hide it from main display
 *
 * @author Xavier VILAIN
 * @package QueryBuilder
 * @license
 *
 */
use Fox\Generator;
use Fox\Msg;
use Fox\Message;
use Fox\Query;
use Fox\Query\Messages;
use Fox\Popup;
use Fox\Html\Button;
use Fox\Html\Block;
use Fox\Html\Screen;
$xview = Xview::instance();
$screen = new Screen("sRemove","delete",Msg(Messages::BUNDLE,Messages::MSG_HIDE_OR_DELETE_QUERY),true,
						Generator::ajaxReload("MC_gQueries"));
$screen->add(new Block("bRemove",null))
		->noLegend();
$screen->add(new Button("ihide",null,Msg(Messages::BUNDLE,Messages::MSG_HIDE_QUERY)));
if($screen->submitted()) {
	$screen->get();
	try {
		$queryid = $xview->val('queryid');
		$query = Fox\Dao::load(Query::className(),$queryid);
		if($screen->pushed() == 'delete') {
			$query->delete();
			echo Msg(Msg::BUNDLE,Msg::MSG_DELETE,$query->label);
		}
		else {
			$idusr = User::current()->getId();
			$queryParam = $query->findParams("$queryid:$idusr");
			$queryParam->setFields(array('idusr' => $idusr,'hidden' => true));
			$query->replaceParams($queryParam);
			$query->prepareUpdate(Query::CHANGE_PARAMS);
			$query->update();
			echo Msg(Messages::BUNDLE,Messages::MSG_HIDDEN,$query->label);
		}
		return;
	}
	catch (Fox\Object\Exception\NotExist $e)
	{
		Message::Error($e->getMessage());
	}
	catch (Fox\Object\Exception\Save $e)
	{
		Message::Error($e->getMessage());
	}
}
$popup = new Popup("remove",
					Msg(Messages::BUNDLE,Messages::LABEL_REMOVE_POPUP_TITLE),
					$screen
			);
if($screen->errors())
{
	echo Xview::instance()->messages();
}
else
{
	echo $popup->display();
}